package cn.cqnu.interceptor;

import cn.cqnu.exception.ParamsException;
import cn.cqnu.global.SystemConstant;
import cn.cqnu.global.SystemReturnEnum;
import cn.cqnu.utils.JWTUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 后端管理拦截器
 *
 * @author Wei
 * @date 2024/01/20
 */
public class AdminInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //    判断jwt中的type字段是否为true
        Map<String, Object> payload = JWTUtils.getPayload(request.getHeader("token"));
        if (payload.get("type") != SystemConstant.TYPE_ADMIN)
            throw new ParamsException(SystemReturnEnum.USER_TYPE_ERR);
        return true;
    }
}
